import Base from 'models/base';
import moment from 'moment';

import Pagination from 'models/pagination';
import CircleZoneLog from 'models/sysCircle/circleZoneLog';

import SystemUtil from 'utils/systemUtil';

const typeColorObj = {
  "种草": "#00c76c",
  "上新": "#fe007f",
  "严选": "#001878",
  "推荐": "#90ee90",
  "爆款": "#ff0000"
};

export default class CircleZone extends Base {
  constructor(data) {
    super();

    this.id = SystemUtil.getId();
    this.spaceId = data.spaceId;
    this.name = data.nickname;
    this.headimgurl = data.headimgurl;
    this.timeDistance = data.timeDistance;
    this.unionid = data.unionid;
    this.isPraise = data.likeCountId !== null;

    let branchId = data.branchId;
    let partnerId = data.partnerId;
    this.hasShop = branchId !== null || partnerId !== null;
    this.branchId = branchId;
    this.partnerId = partnerId;

    let typeTag = data.typeTag;
    typeTag = JSON.parse(typeTag);
    typeTag = typeTag[0];
    this.typeTag = typeTag;
    this.typeColor = typeColorObj[typeTag];

    let themeTag = data.themeTag;
    themeTag = JSON.parse(themeTag);
    this.themeTag = themeTag[0];

    let createTime = this.createTime;
    this.createTime = moment(createTime).valueOf();

    let content = data.content;
    if (SystemUtil.isEmpty(content)) {
      content = '';
    }
    this.content = content;

    let pics = data.pics;
    let videos = data.videos;
    let uploadPics = [],
      uploadVideo = '',
      uploadType = '';
    if (!SystemUtil.isEmpty(pics)) {
      uploadPics = JSON.parse(pics);
      uploadType = 'pic';
    } else if (!SystemUtil.isEmpty(videos)) {
      uploadVideo = JSON.parse(videos);
      uploadVideo = uploadVideo[0];
      uploadType = 'video';
    }
    this.pics = uploadPics;
    this.video = uploadVideo;
    this.uploadType = uploadType;

    this.location = data.location;
    this.longitude = data.longitude;
    this.latitude = data.latitude;

    this.pagination = new Pagination();
    this.setStatData(data);

    this.logMap = {};
    this.setCircleLog(data.spaceLogs10, true);
  }

  setStatData(data) {
    let readCount = data.readCount;
    if (SystemUtil.isEmpty(readCount)) {
      readCount = 0;
    }
    this.readCount = readCount;

    let praiseCount = data.likeCount;
    if (SystemUtil.isEmpty(praiseCount)) {
      praiseCount = 0;
    }
    this.praiseCount = praiseCount;

    let pagination = this.pagination;
    let spaceLogCount = data.spaceLogsRows;
    this.spaceLogCount = spaceLogCount;
    pagination.totalCount = spaceLogCount;
  }

  resetCircleStatusData(data) {
    this.isPraise = data.likeCountId !== null;
    this.setStatData(data);
  }

  setCircleLog(logList, isInit = false, pagination = null) {
    if (isInit) {
      this.logMap = {};
    }

    let logs = [];
    let logMap = this.logMap;
    for (let i = 0; i < logList.length; i++) {
      let log = logList[i];
      let logId = log.manliSpaceLogId;
      if (logMap[logId] !== undefined) {
        continue;
      }

      log = new CircleZoneLog(log);
      logMap[logId] = log;
      logs.push(log);
    }
    if (isInit) {
      this.logList = logs;
    } else {
      this.logList.push(...logs)
    }

    if (pagination !== null) {
      this.pagination = pagination;
    }
  }

  resetCircleLogStatusData(logData) {
    let logMap = this.logMap;
    let log = logMap[logData.manliSpaceLogId];
    if (log === undefined) {
      return;
    }
    log.resetCircleLogStatusData(logData);
  }

  getShopData() {
    let partnerId = this.partnerId;
    if (partnerId !== null) {
      return {
        partnerId
      };
    }

    return {
      branchId: this.branchId
    };
  }
}
